package com.cloudcc.frame.Interface;

import com.cloudcc.deveditor.core.CCObject;
import com.cloudcc.deveditor.core.CCService;
import com.cloudcc.deveditor.core.ServiceResult;
import com.cloudcc.deveditor.core.UserInfo;
import com.cloudcc.frame.Utils.CompanyBaseInfoService;
import com.cloudcc.frame.Utils.CompanyInfoCountService;
import net.sf.json.JSONObject;

import javax.print.attribute.standard.NumberUp;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.UUID;

public class orderopeningTest {

    public void tsets(HttpServletRequest request) {


        /*开函客户的阶段变为 	转换成功 ,  给项目经理发送通知， 商机的升迁 ， 开函数据的储存   客户的修改为电子保函客户 */
        //这是一个Site页面，可以通过URL直接访问 上面是测试环境
//        ServiceResult sr = CCService.getUserInfo("61ee0c8c32fe6ea38850a3475fa15750fbc1bb1a20768b71", "test_hhtest@cloudcc.com", "111111");
              ServiceResult sr = null;
        //ServiceResult sr = CCService.getUserInfo("f6c729c175119d3aa2942d814a63df8564dfe85e8959db2d","interface@cloudcc.com","111111");
        UserInfo uuserInfo = (UserInfo) sr.get("COMMON_USERINFO");
        String resultMessage = "SUCCESS";
        String resultMessageError = "ERROR";
        String resultCode = "0000";
        String ACCOUNTTYPE = "转换成功";
        String ELEOPPTYPE = "4-落地开函";
        String ELEOPPTYPE3 = "3-客情调研";
        String TURETYPE = "已完结";
        String ACCOUNTTTYPT = "待分配";
        String GHXZJD = "已失效";
        String GHXZJD2 = "已生效";
        String ACCOUNTRECORDTYPE = "20211C59581DC5CZOn5N";
        /*公海池用户*/
        String gzghc = "00520209EEC8AF4CDHbS";
        String msg = "";
        CCService cs = new CCService(uuserInfo);

        String dataInfo = request.getParameter("data") == null ? "" : request.getParameter("data") + "";
        String invokeId = request.getParameter("invokeId") == null ? "" : request.getParameter("invokeId") + "";
        /*插入接口调用记录*/
        CCObject interface_record = new CCObject("interface_record");
        interface_record.put("url", "orderopening.jsp");
        interface_record.put("record_ID", invokeId);
        interface_record.put("datas", dataInfo);
        try {
            JSONObject jsonObject = JSONObject.fromObject(dataInfo);
            String cusCertNo = jsonObject.get("cusCertNo") == null ? "" : jsonObject.get("cusCertNo") + "";
            String lgNo = jsonObject.get("lgNo") == null ? "" : jsonObject.get("lgNo") + "";
            String customerName = jsonObject.get("customerName") == null ? "" : jsonObject.get("customerName") + "";
            String guaranteeCreateDate = jsonObject.get("guaranteeCreateDate") == null ? "" : jsonObject.get("guaranteeCreateDate") + "";
            String platformCode = jsonObject.get("platformCode") == null ? "" : jsonObject.get("platformCode") + "";
            String actualAmount = jsonObject.get("actualAmount") == null ? "" : jsonObject.get("actualAmount") + "";
            String guaranteeAmount = jsonObject.get("guaranteeAmount") == null ? "" : jsonObject.get("guaranteeAmount") + "";
            String operatorName = jsonObject.get("operatorName") == null ? "" : jsonObject.get("operatorName") + "";
            String operatorPhone = jsonObject.get("operatorPhone") == null ? "" : jsonObject.get("operatorPhone") + "";
            String findAccountIdSql = "select id,ownerid from account where  invoiceTaxNo ='" + cusCertNo + "' and is_deleted='0' ";
            List<CCObject> accountList = cs.cqlQuery("account", findAccountIdSql);

            String accountId = "";
            String userId = "";
            String  fzrId ="";
            if (accountList.size() > 0) {
                String findSqlpsmc = "select id from psmc where uncode ='" + platformCode + "' and is_deleted='0' ";
                List<CCObject> ccObjects = cs.cqlQuery("psmc", findSqlpsmc);
                String findAccount = "select id,fk_ccuser from manage_group where  fk_account ='" + accountId + "' and fk_psmc ='" + ccObjects.get(0).get("id") + "' and is_deleted='0' and ghzt!='" + GHXZJD + "'";

                List<CCObject> manageGroupInfo = cs.cqlQuery("manage_group", findAccount);
                if (manageGroupInfo.size() > 0) {
                    for (int i = 0; i < manageGroupInfo.size(); i++) {
                        String groupId = manageGroupInfo.get(0).get("id") == null ? "" : manageGroupInfo.get(0).get("id") + "";
                        String update = "update manage_group set ghzt='" + GHXZJD2 + "' where id ='" + groupId + "' ";
                        cs.cqlQuery("manage_group", update);
                        userId = manageGroupInfo.get(0).get("fk_ccuser")==null?"": manageGroupInfo.get(0).get("fk_ccuser")+"";
                    }
                }
                /*更改客户的阶段 加 记录类型*/
                String updateAccountTypeSql = "update account set follow_status = '" + ACCOUNTTYPE + "',recordtype ='" + ACCOUNTRECORDTYPE + "',deal_status='已开函' where id ='" + accountId + "' ";
                cs.cqlQuery("account", updateAccountTypeSql);

                /*商机的升迁*/
                String updateEleOppType = "update eleopp set jieduan = '" + ELEOPPTYPE + "',status1='" + TURETYPE + "',open_time='" + guaranteeCreateDate + "' where khmc ='" + accountId + "' and jieduan = '" + ELEOPPTYPE3 + "' and is_deleted='0' ";
                //                String updateEleOppType ="update eleopp set jieduan = '"+ELEOPPTYPE+"' where khmc ='"+accountId+"' and jieduan = '"+ELEOPPTYPE3+"' and is_deleted='0' ";
                cs.cqlQuery("eleopp", updateEleOppType);

                /*电子保函开函信息*/
                CCObject manageGroup = new CCObject("ele_order");
                manageGroup.put("fk_account", accountId);
                manageGroup.put("name", lgNo);
                manageGroup.put("fk_ccuser", userId);
                manageGroup.put("open_time", guaranteeCreateDate);
                manageGroup.put("get_money", actualAmount);
                manageGroup.put("g_amount", guaranteeAmount);
                manageGroup.put("ownerid", fzrId);
                manageGroup.put("jbr", operatorName);
                String findSql = "select id from psmc where uncode ='" + platformCode + "' and is_deleted='0' ";
                List<CCObject> psmc = cs.cqlQuery("psmc", findSql);
                if (psmc.size() > 0) {
                    manageGroup.put("fk_psmc", psmc.get(0).get("id"));
                }

                manageGroup.put("operatorPhone", operatorPhone);
                cs.insert(manageGroup);


                /* 发送开函通知 插入通知表*/
                CCObject object = new CCObject("notice");
                object.put("companyName", customerName);
                object.put("accountId", userId);
                cs.insert(object);


                JSONObject returnDataInfo = new JSONObject();
                returnDataInfo.put("invokeId", invokeId);
                returnDataInfo.put("resultCode", resultCode);
                returnDataInfo.put("resultMessage", resultMessage);
                interface_record.put("requestDataLong", returnDataInfo);
                cs.insert(interface_record);
                msg = String.valueOf(returnDataInfo);

            } else {

                /*还需要查询公招信息和工商信息*/
                JSONObject requestDataInfo = new JSONObject();
                requestDataInfo.put("invokeId", invokeId);
                JSONObject dataInfos = new JSONObject();
                dataInfos.put("creditCode", cusCertNo);
                requestDataInfo.put("data", dataInfo);


                /*公招信息*/
                try {
                    /*公招信息*/
                    CompanyInfoCountService companyInfoCountService = new CompanyInfoCountService(uuserInfo);
                    String s = companyInfoCountService.sendPost(String.valueOf(requestDataInfo));
                    JSONObject jsonObject1 = JSONObject.fromObject(s);
                    JSONObject jsonObjectInfo = jsonObject1.getJSONObject("data");
                    CCObject eleOpenInfo = new CCObject("ele_open_info ");
                    eleOpenInfo.put("first_con_num", jsonObjectInfo.get("registeredConstructorsFirst"));
                    eleOpenInfo.put("second_con_num", jsonObjectInfo.get("registeredConstructorsSecond"));
                    eleOpenInfo.put("gczzsl", jsonObjectInfo.get("engineeringQualifications"));
                    eleOpenInfo.put("main_area", jsonObjectInfo.get("mainBiddingArea"));
                    eleOpenInfo.put("main_type", jsonObjectInfo.get("projectCategoryMoreThenFifty"));
                    cs.insert(eleOpenInfo);
                } catch (Exception e) {
                }


                /*公商信息*/
                CompanyBaseInfoService information = new CompanyBaseInfoService(uuserInfo);
                net.sf.json.JSONObject requsetInfo1 = new net.sf.json.JSONObject();
                net.sf.json.JSONObject requsetInfo = new net.sf.json.JSONObject();

                requsetInfo1.put("invokeId", invokeId);
                requsetInfo.put("creditCode", cusCertNo);
                requsetInfo1.put("data", requsetInfo);
                String informationInfoList = information.sendPost(String.valueOf(requsetInfo1));
                try {
                    JSONObject informationInfo = JSONObject.fromObject(informationInfoList).getJSONObject("data");
                    /*客户及工商信息*/
                    CCObject accountInfo = new CCObject("Account");
                    String companyName = informationInfo.getString("companyName") == null ? "" : informationInfo.getString("companyName") + "";
                    accountInfo.put("name", companyName);
                    accountInfo.put("invoiceTaxNo", cusCertNo);
                    String address = informationInfo.getString("regAddress") == null ? "" : informationInfo.getString("regAddress") + "";
                    String legaRepresentative = informationInfo.getString("legaRepresentative") == null ? "" : informationInfo.getString("legaRepresentative") + "";
                    accountInfo.put("registeredAddress", address);
                    accountInfo.put("full_name", legaRepresentative);
                    String establishmentDate = informationInfo.getString("establishmentDate") == null ? "" : informationInfo.getString("establishmentDate") + "";
                    accountInfo.put("establishDate", establishmentDate);
                    String province = informationInfo.getString("province") == null ? "" : informationInfo.getString("province") + "";
                    accountInfo.put("province", province);
                    String cityCodeHh = informationInfo.getString("cityCodeHh") == null ? "" : informationInfo.getString("cityCodeHh") + "";
                    accountInfo.put("fk_city_name", cityCodeHh);
                    String countyCodeHh = informationInfo.getString("countyCodeHh") == null ? "" : informationInfo.getString("countyCodeHh") + "";
                    accountInfo.put("fk_country_name", countyCodeHh);
                    String companyType = informationInfo.getString("companyType") == null ? "" : informationInfo.getString("companyType") + "";
                    accountInfo.put("qyxz", companyType);
                    accountInfo.put("recordtype", ACCOUNTRECORDTYPE);
                    accountInfo.put("follow_status", ACCOUNTTTYPT);
                    accountInfo.put("ownerid", gzghc);
                    accountInfo.put("deal_status", "已开函");
                    String registeredCapital = informationInfo.getString("registeredCapital") == null ? "" : informationInfo.getString("registeredCapital") + "";
                    accountInfo.put("registered_capital", registeredCapital);

                    /*插入客户*/
                    ServiceResult insert = cs.insert(accountInfo);
                    String accountIdInfo = insert.get("id") == null ? "" : insert.get("id") + "";
                    //                String update = "update Account set recordtype ='"+ACCOUNTRECORDTYPE+"' where id ='"+accountIdInfo+"' ";
                    //                cs.cqlQuery("Account",update);
                    /*电子保函开函信息*/
                    CCObject manageGroup = new CCObject("ele_order");
                    manageGroup.put("fk_account", accountIdInfo);
                    manageGroup.put("name", lgNo);
                    manageGroup.put("fk_ccuser", userId);
                    manageGroup.put("open_time", guaranteeCreateDate);
                    manageGroup.put("get_money", actualAmount);
                    manageGroup.put("g_amount", guaranteeAmount);
                    manageGroup.put("jbr", operatorName);
                    String findSql = "select id from psmc where uncode ='" + platformCode + "' and is_deleted='0' ";
                    List<CCObject> psmc = cs.cqlQuery("psmc", findSql);
                    if (psmc.size() > 0) {
                        manageGroup.put("fk_psmc", psmc.get(0).get("id"));
                    }
                    manageGroup.put("operatorPhone", operatorPhone);
                    cs.insert(manageGroup);
                    JSONObject returnDataInfo = new JSONObject();
                    returnDataInfo.put("invokeId", insert.get("id"));
                    returnDataInfo.put("resultCode", resultCode);
                    returnDataInfo.put("resultMessage", resultMessage);
                    msg = String.valueOf(returnDataInfo);
                } catch (Exception e) {

                    String resultMessageInfo = JSONObject.fromObject(informationInfoList).getString("resultMessage");
                    JSONObject returnDataInfo = new JSONObject();
                    returnDataInfo.put("invokeId", invokeId);
                    returnDataInfo.put("resultCode", resultCode);
                    returnDataInfo.put("resultMessage", resultMessageInfo);
                    interface_record.put("requestDataLong", returnDataInfo);
                    cs.insert(interface_record);
                    msg = String.valueOf(returnDataInfo);
                }

            }
            request.setAttribute("jsonmsg", msg);

        } catch (Exception e) {
            JSONObject returnDataInfo = new JSONObject();
            returnDataInfo.put("invokeId", invokeId);
            returnDataInfo.put("resultCode", resultCode);
            returnDataInfo.put("resultMessage", resultMessageError+e);
            interface_record.put("requestDataLong", returnDataInfo);
            cs.insert(interface_record);
            msg = String.valueOf(returnDataInfo);
            request.setAttribute("jsonmsg", msg);
        }


    }

}
